home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Magazine / UsingPDF / GhostScript / source / gs5.10 / gsdsrc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-01  |  3.2 KB  |  108 lines

  1. /* Copyright (C) 1997 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gsdsrc.c */
  20. /* DataSource procedures */
  21.  
  22. #include "memory_.h"
  23. #include "gx.h"
  24. #include "gsdsrc.h"
  25. #include "gserrors.h"
  26. #include "stream.h"
  27.  
  28. /* GC descriptor */
  29. public_st_data_source();
  30. #define psrc ((gs_data_source_t *)vptr)
  31. private ENUM_PTRS_BEGIN(data_source_enum_ptrs) {
  32.     if ( psrc->access == data_source_access_string )
  33.       ENUM_RETURN_CONST_STRING_PTR(gs_data_source_t, data.str);
  34.     else if ( psrc->access == data_source_access_bytes )
  35.       ENUM_RETURN_PTR(gs_data_source_t, data.str.data);
  36.     else /*if ( psrc->access == data_source_access_stream )*/
  37.       ENUM_RETURN_PTR(gs_data_source_t, data.strm);
  38. } ENUM_PTRS_END
  39. private RELOC_PTRS_BEGIN(data_source_reloc_ptrs) {
  40.     if ( psrc->access == data_source_access_string )
  41.       RELOC_CONST_STRING_PTR(gs_data_source_t, data.str);
  42.     else if ( psrc->access == data_source_access_bytes )
  43.       RELOC_PTR(gs_data_source_t, data.str.data);
  44.     else /*if ( psrc->access == data_source_access_stream )*/
  45.       RELOC_PTR(gs_data_source_t, data.strm);
  46. } RELOC_PTRS_END
  47. #undef psrc
  48.  
  49. /* Access data from a string or a byte object. */
  50. /* Does *not* check bounds. */
  51. int
  52. data_source_access_string(const gs_data_source_t *psrc, ulong start,
  53.   uint length, byte *buf, const byte **ptr)
  54. {    const byte *p = psrc->data.str.data + start;
  55.  
  56.     if ( ptr )
  57.       *ptr = p;
  58.     else
  59.       memcpy(buf, p, length);
  60.     return 0;
  61. }
  62. /* access_bytes is identical to access_string, but has a different */
  63. /* GC procedure. */
  64. int
  65. data_source_access_bytes(const gs_data_source_t *psrc, ulong start,
  66.   uint length, byte *buf, const byte **ptr)
  67. {    const byte *p = psrc->data.str.data + start;
  68.  
  69.     if ( ptr )
  70.       *ptr = p;
  71.     else
  72.       memcpy(buf, p, length);
  73.     return 0;
  74. }
  75.  
  76. /* Access data from a stream. */
  77. /* Returns gs_error_rangecheck if out of bounds. */
  78. int
  79. data_source_access_stream(const gs_data_source_t *psrc, ulong start,
  80.   uint length, byte *buf, const byte **ptr)
  81. {    stream *s = psrc->data.strm;
  82.     const byte *p;
  83.  
  84.     if ( start >= s->position &&
  85.          (p = start - s->position + s->cbuf) + length <=
  86.            s->cursor.r.limit + 1
  87.        ) {
  88.       if ( ptr )
  89.         *ptr = p;
  90.       else
  91.         memcpy(buf, p, length);
  92.     } else {
  93.       uint nread;
  94.       int code = sseek(s, start);
  95.  
  96.       if ( code < 0 )
  97.         return_error(gs_error_rangecheck);
  98.       code = sgets(s, buf, length, &nread);
  99.       if ( code < 0 )
  100.         return_error(gs_error_rangecheck);
  101.       if ( nread != length )
  102.         return_error(gs_error_rangecheck);
  103.       if ( ptr )
  104.         *ptr = buf;
  105.     }
  106.     return 0;
  107. }
  108.